package client.comm.baoding.ui

import android.content.Intent
import android.view.View
import android.view.ViewGroup
import android.webkit.WebSettings
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.GoodsDetailAdapter
import client.comm.baoding.adapter.GoodsGylDetailAdapter
import client.comm.baoding.api.bean.GoodsDetail
import client.comm.baoding.api.bean.ShopingBean
import client.comm.baoding.databinding.ActivityGoodsgyldetailBinding
import client.comm.baoding.dialog.SelectGylSkuDialog
import client.comm.baoding.ui.vm.GoodsGylDetailModel
import client.comm.commlib.base.SyBannerAdapter
import client.comm.commlib.getScreenWidth
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import com.bumptech.glide.request.RequestOptions
import com.youth.banner.indicator.CircleIndicator

class GoodsGylDetailActivity :
    BaseMiddleActivity<GoodsGylDetailModel, ActivityGoodsgyldetailBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_goodsgyldetail
    }

    val adapter: GoodsGylDetailAdapter by lazy {
        GoodsGylDetailAdapter(this, mImgLoader)
    }

    override fun initViewModel(): GoodsGylDetailModel = createViewModel()

    override fun initView() {
        binding.vm = viewModel
        binding.event = this
        binding.recyclerView.adapter = adapter

        binding.emptyLayout.setOnClickListener {
            intent?.getStringExtra("goodsId")?.let { it1 ->
                viewModel.holdCall(
                    it1,
                )
            }
        }
        intent?.getStringExtra("goodsId")?.let {
            viewModel.holdCall(
                it,
            )
        }
    }

    override fun observerUI() {
        viewModel.goodsDetail.observe(this, {
            it?.let {
                if (it.isSuccess && it.data != null) {
                    it.data?.let {
                        if (it.goods_detail != null && it.goods_detail.size > 0)
                            adapter.setData(it.goods_detail as MutableList<String?>, it)
                        else {
                            adapter.setData(ArrayList<String?>(), it)
                        }
                    }
                    if (intent.getBooleanExtra("open", false)) {
                        ljgm()
                    }
                    binding.emptyLayout.visibility = View.GONE
                } else {
                    if (it.code == 400) {
                        finish()
                    }
                    toast(it.message)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

        viewModel.buyRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("加入购物车成功")
                } else {
                    toast(it.message)
                }
            }
        })
    }

    fun ljgm() {
        if (viewModel.isShowing.value!!)
            return
        SelectGylSkuDialog(this, "").apply {
            id = viewModel.goodsDetail.value!!.data!!.goods_id
            goods = viewModel.goodsDetail.value!!.data!!


//            lpq_price = viewModel.goodsDetail.value!!.data!!.lpq_price
//            integral = viewModel.goodsDetail.value!!.data!!.integral

            imgUrl = "${viewModel.goodsDetail.value!!.data!!.goods_img}"
            name = "${viewModel.goodsDetail.value!!.data!!.name}"
            ok = { it, sku ->
                var num = 0

                try {
                    num = it.toInt()
                } catch (e: Exception) {
                }

                if (num > 0) {
                    val list = ArrayList<ShopingBean>()
                    val bean = ShopingBean(
                        goods_id = id,
                        goods_img = sku!!.goodsImage,
                        name = name,
                        price = sku!!.goodsMarkprice,
                        shopping_id = "",
                        num = it.toInt(),
                        store_id = "",
                        sku_id = sku!!.id,
                        sku = sku!!.goodsSpec,
//                        old_price = viewModel.goodsDetail.value!!.data!!.old_price,
//                        lpq_price = viewModel.goodsDetail.value!!.data!!.lpq_price,

                        integral = 0.0,
                        medal_price = 0.0,
                        medal_num = sku!!.medalNum
//                        price_r = viewModel.goodsDetail.value!!.data!!.price_r,
//                        create_time = viewModel.goodsDetail.value!!.data!!.create_time,
                    )
                    list.add(bean)
                    startActivity(
                        Intent(
                            this@GoodsGylDetailActivity,
                            ConfirmOrderActivity::class.java
                        ).apply {
                            putExtra("list", list)
                            putExtra("addrIndex", 1)
                        })
                    finish()
                } else {
                    toast("请输入数量")
                }


            }
        }.show()
    }

}